package com.cicc.beergamevirtualsimulationsystem.common.webSocket.handler.impl;

import cn.hutool.json.JSONUtil;
import com.cicc.beergamevirtualsimulationsystem.common.webSocket.config.WebSocketMessageSender;
import com.cicc.beergamevirtualsimulationsystem.common.webSocket.handler.JsonMessageHandler;
import com.cicc.beergamevirtualsimulationsystem.common.webSocket.message.Impl.InitRoomJsonWebSocketMessage;
import com.cicc.beergamevirtualsimulationsystem.common.webSocket.message.WebSocketMessageTypeEnum;
import com.cicc.beergamevirtualsimulationsystem.entity.RoomInfoManager;
import com.cicc.beergamevirtualsimulationsystem.model.parameter.dto.InnerGameParameterDto;
import com.cicc.beergamevirtualsimulationsystem.model.parameter.dto.InnerGameParameterWebSocketDto;
import com.cicc.beergamevirtualsimulationsystem.model.parameter.req.InnerGameParameterUpdateReq;
import com.cicc.beergamevirtualsimulationsystem.service.ParameterService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

@Component
public class InnerGameParameterUpdateJsonMessageHandler implements JsonMessageHandler<InitRoomJsonWebSocketMessage> {


    @Resource
    private ParameterService parameterService;

    @Override
    public void handle(WebSocketSession session, InitRoomJsonWebSocketMessage message) {
        String roomId = session.getAttributes().get("roomId").toString();
        String userId = session.getAttributes().get("userId").toString();

        String changeParameterDate = message.getMessage();
        InnerGameParameterUpdateReq bean = JSONUtil.toBean(changeParameterDate, InnerGameParameterUpdateReq.class);
        bean.setRoomId(roomId);

        try {
//            InnerGameParameterDto innerGameParameterDto = parameterService.updateInnerGameParameter(bean);
            ;
//            InnerGameParameterWebSocketDto innerGameParameterWebSocketDto = new InnerGameParameterWebSocketDto();
//
//            innerGameParameterWebSocketDto.setGameTime(innerGameParameterDto.getGameTime());
//            innerGameParameterWebSocketDto.setCycles(innerGameParameterDto.getCycles());
//            innerGameParameterWebSocketDto.setRoomId(innerGameParameterDto.getRoomId());
//            innerGameParameterWebSocketDto.setChange(true);

            RoomInfoManager.updateRoomConfig(session,roomId,bean);
        }
        catch (Exception e) {
            WebSocketMessageSender.send(session, "更新失败");
        }


    }

    @Override
    public String type() {
        return WebSocketMessageTypeEnum.INNER_GAME_PARAMETER_UPDATE.getValue();
    }

    @Override
    public Class<InitRoomJsonWebSocketMessage> getMessageClass() {
        return InitRoomJsonWebSocketMessage.class;
    }
}
